package com.sq.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sq.common.enums.ReqTypeEnum;
import com.sq.common.exception.param.ParamNotComplete;
import com.sq.common.exception.param.RepeatDataException;
import com.sq.common.utils.ParamUtil;
import com.sq.common.utils.StringUtils;
import com.sq.system.mapper.SysMenuMapper;
import com.sq.system.pojo.SysMenu;
import com.sq.system.pojo.SysRole;
import com.sq.system.service.SysMenuService;
import com.sq.system.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.retry.Repeat;

import java.util.*;

/**
 *
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
    implements SysMenuService
{

    @Autowired
    private SysMenuMapper sysMenuMapper;


    @Autowired
    private SysRoleService sysRoleService;

    @Override
    public List<SysMenu> selectRoleMenuByUserId(String userId) {

        if (StringUtils.isBlank(userId)){
            throw new ParamNotComplete("userId is null");
        }

        try{
            List<SysMenu> sysMenus = sysMenuMapper.selectRoleMenuByRoleIds(sysRoleService.selectRoleByUserId(userId));
            return sysMenus;
        }catch (Exception e){
            throw new RuntimeException();
        }

    }

    @Override
    public List<SysMenu> selectRoleMenuByUserRoles(List<SysRole> sysRoles) {

        if (Objects.isNull(sysRoles) || sysRoles.isEmpty()){
            throw new ParamNotComplete("用户角色列表为空");
        }
        return sysMenuMapper.selectRoleMenuByRoleIds(sysRoles);
    }

    @Override
    public Set<String> selectRolePermissions(List<SysRole> sysRoles) {
        if (Objects.isNull(sysRoles) || sysRoles.isEmpty()){
            throw new ParamNotComplete("用户角色列表为空");
        }
        return sysMenuMapper.selectRoleMenuCodeByRoleIds(sysRoles);
    }

    @Override
    public List<SysMenu> selectRoleMenuTreeByUsername(String username) {
        List<SysMenu> sysMenus = selectRoleMenuByUserId(username);
        return menuToTree(sysMenus);
    }

    public List<SysMenu> menuToTree(List<SysMenu> lists){

        List<SysMenu> sysMenus = new ArrayList<>();
        Iterator<SysMenu> iterator = lists.iterator();
        while (iterator.hasNext()){
            SysMenu sysMenu = iterator.next();
            if (sysMenu.getParentId() == 0){
                getChildren(sysMenu,lists);
                sysMenus.add(sysMenu);
            }
        }
        return sysMenus;

    }

    private void getChildren(SysMenu sysMenu, List<SysMenu> lists)     {
        // 得到子节点列表
        List<SysMenu> childList = getChildList(lists, sysMenu);
        sysMenu.setChildrenMenu(childList);
        for (SysMenu tChild : childList)
        {
            if (hasChild(lists, tChild))
            {
                getChildren(tChild, lists);
            }
        }
    }

    private List<SysMenu> getChildList(List<SysMenu> list, SysMenu t)
    {
        List<SysMenu> tlist = new ArrayList<SysMenu>();
        Iterator<SysMenu> it = list.iterator();
        while (it.hasNext())
        {
            SysMenu n = (SysMenu) it.next();
            if (n.getParentId().longValue() == t.getMenuId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    private boolean hasChild(List<SysMenu> list, SysMenu t)
    {
        return getChildList(list, t).size() > 0;
    }


    @Override
    public List<SysMenu> selectAllMenu(SysMenu sysMenu) {
        return sysMenuMapper.selectAllMenu(sysMenu);
    }


    @Override
    public boolean saveSysMenu(SysMenu sysMenu) {
        // 判断参数
        ParamUtil.checkParam(SysMenu.class, ReqTypeEnum.insert);

        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("menu_name",sysMenu.getMenuName());
        SysMenu menu = getOne(queryWrapper);
        if (!Objects.isNull(menu)){
            throw new RepeatDataException(sysMenu.getMenuName() + "已存在");
        }

        // 添加参数
        sysMenu.setCreateTime(new Date());
        sysMenu.setCreateBy("");

        return save(sysMenu);

    }

    @Override
    public boolean updateMenuById(SysMenu sysMenu) {
        // 判断参数
        ParamUtil.checkParam(SysMenu.class, ReqTypeEnum.update);
        sysMenu.setUpdateBy("");
        sysMenu.setUpdateTime(new Date());
        return updateById(sysMenu);
    }

    @Override
    public boolean delMenuById(Long id) {
        return removeById(id);
    }


    @Override
    public SysMenu selectOneMenu(Long menuId) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("menu_id",menuId);
        return  getOne(queryWrapper);
    }

    @Override
    public List<SysMenu> selectAllMenuTree() {
        List<SysMenu> sysMenus = selectAllMenu(new SysMenu());
        if (Objects.isNull(sysMenus) || sysMenus.isEmpty()){
            return sysMenus;
        }
        return menuToTree(sysMenus);
    }
}




